let wxMethods = {
    // 获取appid
    getAppid() {
        $.post("/api/wxContent/appId", {
            company_id
        }, function (result) {
            wxMethods.getCode(result.data.corpid, result.data.agentid);
        });
        return false;
    },
    // 获取code
    getCode(appid, agentid) {
        var host = window.location.protocol + "//" + window.location.host;
        var url = encodeURI(host + "/chat-tool/oauth2.html?id=" + company_id);
        var href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_privateinfo&agentid=${agentid}&state=#wechat_redirect`;
        window.location.href = href;
    },

    getUrlCode() {
        // 截取url中的code方法
        var url = location.search;
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
            }
        }
        return theRequest;
    },

    // 获取userID
    getmyId() {
        $.post("/api/user", {
            company_id, code
        }, function (result) {
            wxMethods.getuserdetail(result.data.user_ticket)
        });
    },

    getuserdetail(user_ticket){
        $.post("/api/Staff/submitInfo", {
            company_id, user_ticket
        }, function (result) {
           if(result.status){
                alert('授权成功,请关闭页面！')
           }else{
                alert('授权失败,请退出重新进入页面！')
           }
            
        });
    }


}
let code = wxMethods.getUrlCode().code,
    company_id = wxMethods.getUrlCode().id;
if (company_id && code) {
    wxMethods.getmyId();
} else {
    wxMethods.getAppid();
}

